<?php
namespace app\common\server;

use traits\think\Instance;
use Requests;

class Tour
{
    use Instance;

    const BASE_URL = 'http://openapi.ynjply.cn';

    /** 景区列表 */
    const METHOD_SCENIC_LISTS      = 'com.tour.openapi.controller.ScenicController.doScenicList';
    /** 景区详情 */
    const METHOD_SCENIC_VIEW       = 'com.tour.openapi.controller.ScenicController.doScenicView';
    /** 联票列表 */
    const METHOD_TICKET_LISTS      = 'com.tour.openapi.controller.TicketController.doTicketList';
    /** 联票详情 */
    const METHOD_TICKET_VIEW       = 'com.tour.openapi.controller.TicketController.doTicketInfo';
    /** 分页获取订单列表 */
    const METHOD_MEMBER_ORDER_LIST = 'com.tour.openapi.controller.OrderControllerNew.doOrderListNew';
    /** 查看订单详细信息 */
    const METHOD_MEMBER_ORDER_VIEW = 'com.tour.openapi.controller.OrderControllerNew.doOrderInfoNew';
    /** 新建景点订单 */
    const METHOD_ORDER_SCENICREATE = 'com.tour.openapi.controller.OrderControllerNew.doCreateScenicOrderNew';
    /** 新建联票订单 */
    const METHOD_ORDER_COMBCREATE  = 'com.tour.openapi.controller.OrderControllerNew.doCreateCombOrderNew';
    /** 新订单撤销 */
    const METHOD_ORDER_CANCEL      = 'com.tour.openapi.controller.OrderControllerNew.doOrderCancelNew';
    /** 账户余额支付订单消费 */
    const METHOD_ORDER_BALANCE     = 'com.tour.openapi.controller.OrderControllerNew.doPayAmtByBalanceNew';
    /** 订单消费退款到企业总账余额 */
    const METHOD_ORDER_REFUND      = 'com.tour.openapi.controller.OrderControllerNew.doRefundTheBalanceNew';
    /** 酒店列表 */
    const METHOD_HOTEL_LIST        = 'com.tour.openapi.controller.HotelController.doHotelList';
    /** 酒店详情 */
    const METHOD_HOTEL_VIEW        = 'com.tour.openapi.controller.HotelController.doHotelInfo';
    /** 创建酒店点订单 */
    const METHOD_ORDER_HOTELCREATE = 'com.tour.openapi.controller.OrderControllerNew.doCreateHotelOrder';
    /** 接受订单信息 */
    const METHOD_ORDER_RECNO       = 'com.tour.openapi.controller.OrderController.doReceiveOrderNo';
    /**
     * 请求接口 url
     * 
     * @var string
     */
    protected $interface = '';

    /**
     * 系统级参数配置
     * @var [type]
     */
    private $_sysParams = [
        'appKey'         => 'a28d936a-b3ed-4770-9b1e-a8356b22a59e',
        'appSecret'      => '0BC9436CD2DC5F48BFCF89DEE6470293',
        'method'         => '',
        'timeStamp'      => '',
        'version'        => '1.0',
        'checkSign'      => ''
    ];

    public function __construct($method = '')
    {
        $this->_sysParams['timeStamp'] = date('Y-m-d H:i:s');
        $this->_sysParams['method'] = $method;
    }

    protected static function headers($method)
    {
        return (new static($method))->_sysParams;
    }

    /**
     * 景区列表
     * URI : /scenic/list
     * @param   string     $orderType  升序： 类型(top,price,grade)+排序方式（Asc/Desc）
     * @param   integer    $pageNumber 分页页号
     * @param   integer    $pageSize   每页记录数
     * @param   string     $grade      景区级别ID 4A 5A
     * @return  json       
     */
    public function scenicLists($orderType = '', $pageNumber = 1, $pageSize = 25, $grade = '')
    {
        $query = [
            'orderType'  => $orderType,
            'pageNumber' => $pageNumber,
            'pageSize'   => $pageSize,
            'grage'      => $grade
        ];
        // $params = $this->_sysParams;
        // static::_method($params, static::METHOD_SCENIC_LISTS);
        $headers = static::headers(static::METHOD_SCENIC_LISTS);
        // static::_sign($params);
        $url = self::BASE_URL.'/scenic/list' . static::buildQueryParams($query);
        return Requests::get($url, $headers)->body;
    }
    /**
     * 景点详情
     * /scenic/view/{id}
     * @param   [type]     $id 景点ID
     * @return  json        
     */
    public function scenicView($id)
    {
        $headers = static::headers(static::METHOD_SCENIC_VIEW);
        $url = self::BASE_URL . '/scenic/view/'. $id;
        // static::_sign($headers);
        return Requests::get($url, $headers)->body;
    }
    /**
     * 联票列表
     * /ticket/list 
     * @param   string     $orderType  排序类型： top/price + Asc/Desc 
     * @param   integer    $pageNumber 分页页码
     * @param   integer    $pageSize   分页记录数
     * @return  json
     */
    public function ticketLists($orderType = 'topAsc', $pageNumber = 2, $pageSize = 25)
    {
        $query = [
            'orderType'  => $orderType,
            'pageNumber' => $pageNumber, 
            'pageSize'   => $pageSize
        ];
        $headers = static::headers(static::METHOD_TICKET_LISTS);
        $url = static::BASE_URL . '/ticket/list' . static::buildQueryParams($query);
        return Requests::get($url, $headers)->body;
    }
    /**
     * 联票详情
     * /ticket/view/{id}
     * @param  [type] $id [description]
     * @return json
     */
    public function ticketView($id)
    {
        $headers = static::headers(static::METHOD_TICKET_VIEW);
        $url    = static::BASE_URL . '/ticket/view/' . $id;
        return Requests::get($url, $headers)->body;
    }
    /**
     * 分页获取订单列表
     * URI ： /member/order/listnew
     * @param   string     $memberusername  用户名
     * @param   string     $orderType       排序类型： top/price + Asc/Desc 
     * @param   integer    $pageNumber      分页页码
     * @param   integer    $pageSize        分页大小
     * @return  json
     */
    public function orderList($memberusername= '',$orderType = '', $pageNumber = 1, $pageSize = 15)
    {
        $query = [
            'memberusername' => $memberusername,
            'orderType'      => $orderType,
            'pageNumber'     => $pageNumber,
            'pageSize'       => $pageSize
        ];
        $headers = static::headers(static::METHOD_MEMBER_ORDER_LIST);
        $url     = static::BASE_URL . '/member/order/listnew' . static::buildQueryParams($query);
        return Requests::get($url, $headers)->body;
    }
    /**
     * 查看订单详情
     * @URI : /member/order/viewnew/
     * @param   [type]     $orderno        订单编号
     * @param   [type]     $memberusername 用户名
     * @return  json
     */
    public function orderView($orderno, $memberusername)
    {
        $headers = static::headers(static::METHOD_MEMBER_ORDER_VIEW);
        $url = static::BASE_URL . '/?s=' . '/member/order/viewnew/' . $orderno; 
        // $url = static::BASE_URL . '/member/order/viewnew/' . $orderno;
        return Requests::get($url, $headers)->body;
    }
    /**
     * 酒店列表
     * URI : /hotel/list
     * @param   [type]     $orderType  排序类型： top/price + Asc/Desc 
     * @param   [type]     $pageNumber 分页页码 
     * @param   [type]     $pageSize   分页大小
     * @param   [type]     $orderdate  房间日期 yyyy-mm-dd
     * @return  [type]                 [description]
     */
    public function hotelList($memUserName, $orderType, $pageNumber, $pageSize, $orderdate = '') 
    {
        $query = [
            'orderType'  => $orderType,
            'pageNumber' => $pageNumber,
            'pageSize'   => $pageSize,
            'orderdate'  => $orderdate
        ];
        $headers = static::headers(static::METHOD_HOTEL_LIST);
        $headers['memUserName'] = $memUserName; 
        dump($headers);
        // $url     = static::BASE_URL . '/' . static::buildQueryParams($query).'/hotel/list';
        $url = static::BASE_URL . '/hotel/list' . static::buildQueryParams($query);       
        // dump($url);die;
        return Requests::get($url, $headers)->body;
    }
    /**
     * 酒店详情
     * URI : /hotel/view
     * @param   [type]     $id 酒店ID
     * @return  [type]         [description]
     */
    public function hotelView($memUserName, $munitid, $orderdate)
    {   

        $headers = static::headers(static::METHOD_HOTEL_VIEW);
        $headers['memUserName'] = $memUserName;
        $url     = static::BASE_URL . '/hotel/view' . static::buildQueryParams(['munitid' =>$munitid, 'orderdate' => $orderdate]);
        return Requests::get($url, $headers)->body;
    }
    //创建酒店点订单
    public function hotelCreate()
    {

    }
    //接收订单点订单
    public function hotelOrderRec()
    {

    }
    private static function _sign(&$data)
    {
        if (isset($data['checkSign'])) {
            unset($data['checkSign']);
        }
        ksort($data);
        $sign = '';
        foreach ($data as $key => $v) {
            $sign .= $key . '=' . $v . '&';
        }
        $sign = substr($sign, 0, -1);
        $sign = base64_encode(md5($sign));
        $data['checkSign'] = $sign;
    }

    private static function _timestamp(array &$data)
    {
        $data['timeStamp'] = date('Y-m-d H:i:s');
    }

    private static function _method(array &$data, $method)
    {
        $data['method'] = $method;
    }

    protected static function buildQueryParams(array $data)
    {
        if (empty($data)) {
            return  '';
        }
        $res = '';
        foreach ($data as $key => $v) {
            $res .= $key . '=' . $v . '&';
        }
        return '?' . substr($res, 0, -1);
    }
}